package com.srwl.mytx.widget;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.github.florent37.viewanimator.AnimationListener;
import com.github.florent37.viewanimator.ViewAnimator;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.srwl.mytx.R;
import com.srwl.mytx.utils.Utils;

import java.util.ArrayList;
import java.util.List;

public class PayPasswordDialog extends BottomSheetDialog implements View.OnClickListener {
    private static final String TAG = "PayPasswordDialog";
    public static final int TYPE_SET_PAY_PASSWORD = 0;//设置支付密码
    public static final int TYPE_INPUT_OLD_PAY_PASSWORD = 1;//输入旧支付密码
    public static final int TYPE_INPUT_NEW_PAY_PASSWORD = 2;//输入新支付密码
    public static final int TYPE_SET_PAY_LIMIT = 3;//设置免密码支付额度
    private Integer type;
    private int originalPayLimit;
    private ImageView iv_key_1, iv_key_2, iv_key_3, iv_key_4, iv_key_5, iv_key_6;//最上方的圆圈,代表输入密码的第1/2/3/4/5/6位
    private TextView tv_payLimit;
    private TextView tv_title;
    private SeekBar sb_payLimit;
    private TextView tv_tips;
    private TextView tv_amount;
    private RelativeLayout rl_tips;
    private List<ImageView> imageViewList = new ArrayList<>();
    private String[] password = new String[6];
    private OnConfirmListener mListener;
    private long lastClickTime;
    private int clickInterval = 200;//删除点击 间隔时间，也是动画持续时间
    private int payLimit;
    private OnSetPassWordListener onSetPasswordListener;
    private String tips;
    private Double amount;

    public PayPasswordDialog(@NonNull Context context) {
        super(context, R.style.BottomDialog);
        init();
        rl_tips.setVisibility(View.GONE);
    }

    public PayPasswordDialog(@NonNull Context context, int type) {
        super(context, R.style.BottomDialog);
        this.type = type;
        init();
        rl_tips.setVisibility(View.GONE);
    }

    public PayPasswordDialog(@NonNull Context context, int type, int payLimit) {
        super(context, R.style.BottomDialog);
        this.type = type;
        originalPayLimit = payLimit;
        init();
        rl_tips.setVisibility(View.GONE);
    }

    public PayPasswordDialog(@NonNull Context context, String tips, Double amount) {
        super(context, R.style.BottomDialog);
        this.tips = tips;
        this.amount = amount;
        init();
        rl_tips.setVisibility(View.VISIBLE);
    }

    void init() {
        setContentView(R.layout.fragment_key_board_dialog);
        tv_payLimit = findViewById(R.id.tv_pay_limit);
        tv_tips = findViewById(R.id.tv_tips);
        tv_amount = findViewById(R.id.tv_amount);
        rl_tips = findViewById(R.id.rl_tips);
        initSeekBar();
        initTitle();
        iv_key_1 = findViewById(R.id.iv_key_1);
        iv_key_2 = findViewById(R.id.iv_key_2);
        iv_key_3 = findViewById(R.id.iv_key_3);
        iv_key_4 = findViewById(R.id.iv_key_4);
        iv_key_5 = findViewById(R.id.iv_key_5);
        iv_key_6 = findViewById(R.id.iv_key_6);
        imageViewList.add(iv_key_1);
        imageViewList.add(iv_key_2);
        imageViewList.add(iv_key_3);
        imageViewList.add(iv_key_4);
        imageViewList.add(iv_key_5);
        imageViewList.add(iv_key_6);
        findViewById(R.id.rl_1).setOnClickListener(this);
        findViewById(R.id.rl_2).setOnClickListener(this);
        findViewById(R.id.rl_3).setOnClickListener(this);
        findViewById(R.id.rl_4).setOnClickListener(this);
        findViewById(R.id.rl_5).setOnClickListener(this);
        findViewById(R.id.rl_6).setOnClickListener(this);
        findViewById(R.id.rl_7).setOnClickListener(this);
        findViewById(R.id.rl_8).setOnClickListener(this);
        findViewById(R.id.rl_9).setOnClickListener(this);
        findViewById(R.id.rl_0).setOnClickListener(this);
        findViewById(R.id.rl_delete).setOnClickListener(this);
        //取消密码输入框的下滑隐藏，容易误触
        this.setCancelable(false);
        this.setCanceledOnTouchOutside(true);
    }


    void initSeekBar() {
        sb_payLimit = findViewById(R.id.sb_pay_limit);

        sb_payLimit.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                tv_payLimit.setText("免密额度：￥" + Utils.moneyFormat(progress));
                payLimit = progress;
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        sb_payLimit.setProgress(1);
    }

    void initTitle() {
        tv_title = findViewById(R.id.tv_title);
        if (type != null) {

            switch (type) {
                case TYPE_SET_PAY_PASSWORD:
                    tv_title.setText("首次设置支付密码和免密额度");
                    //首次设置支付密码的时候，也可以设置免密额度，
                    tv_payLimit.setVisibility(View.VISIBLE);
                    sb_payLimit.setVisibility(View.VISIBLE);
                    break;

                case TYPE_INPUT_OLD_PAY_PASSWORD:
                    tv_tips.setText("修改支付密码");
                    tv_amount.setVisibility(View.GONE);
                    tv_title.setText("输入原支付密码");//输入旧密码没必要显示 seekBar
                    tv_payLimit.setVisibility(View.GONE);
                    sb_payLimit.setVisibility(View.GONE);
                    break;
                case TYPE_INPUT_NEW_PAY_PASSWORD:
                    tv_tips.setText("修改支付密码");
                    tv_amount.setVisibility(View.GONE);
                    tv_payLimit.setVisibility(View.GONE);
                    tv_title.setText("输入新支付密码");
                    break;
                case TYPE_SET_PAY_LIMIT:
                    tv_title.setText("输入支付密码");
                    tv_payLimit.setVisibility(View.VISIBLE);
                    sb_payLimit.setVisibility(View.VISIBLE);
                    sb_payLimit.setProgress(originalPayLimit);
                    break;
                default:
                    tv_title.setText("输入支付密码");
                    tv_payLimit.setVisibility(View.GONE);
                    sb_payLimit.setVisibility(View.GONE);
            }
        } else {
            if (tips == null) {
                tv_tips.setVisibility(View.GONE);
            } else {
                tv_tips.setText(tips);
            }
            if (amount == null) {
                tv_amount.setVisibility(View.GONE);
            } else {
                tv_amount.setText(Utils.moneyFormat(amount));
            }
            tv_title.setText("输入支付密码");
            tv_payLimit.setVisibility(View.GONE);
            sb_payLimit.setVisibility(View.GONE);

        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_1:
                addKey("1");
                break;
            case R.id.rl_2:
                addKey("2");
                break;
            case R.id.rl_3:

                addKey("3");
                break;
            case R.id.rl_4:

                addKey("4");
                break;
            case R.id.rl_5:

                addKey("5");
                break;
            case R.id.rl_6:

                addKey("6");
                break;
            case R.id.rl_7:

                addKey("7");
                break;
            case R.id.rl_8:

                addKey("8");
                break;
            case R.id.rl_9:

                addKey("9");
                break;
            case R.id.rl_0:

                addKey("0");
                break;
            case R.id.rl_delete:
                long currentTime = System.currentTimeMillis();
                if (currentTime - lastClickTime < 200) {
                    return;
                }
                lastClickTime = currentTime;
                removeKey();
                break;
            /*case R.id.rl_commit:
                commit();
                break;*/

        }
    }

    void addKey(String key) {
        for (int i = 0; i < password.length; i++) {
            if (TextUtils.isEmpty(password[i])) {
                password[i] = key;
                break;
            }
        }

        for (ImageView iv_key : imageViewList) {
            if (!iv_key.isShown()) {
                iv_key.setVisibility(View.VISIBLE);
                ViewAnimator.animate(iv_key).zoomIn().duration(clickInterval).start();
                break;
            }
        }
        //当输入密码的最后一位数不为空则代表已经密码输入完成，直接验证密码
        if (!TextUtils.isEmpty(password[password.length - 1])) {
            commit();
        }
    }

    /**
     * 倒序遍历password，遇到第一个有值的，清除，跳出循环...
     */
    void removeKey() {

        for (int i = password.length - 1; i >= 0; i--) {
            if (!TextUtils.isEmpty(password[i])) {
                password[i] = "";
                break;
            }
        }

        for (int i = imageViewList.size() - 1; i >= 0; i--) {
            if (imageViewList.get(i).isShown()) {
                final View view = imageViewList.get(i);
                ViewAnimator.animate(view).zoomOut().duration(clickInterval).onStop(new AnimationListener.Stop() {
                    @Override
                    public void onStop() {
                        view.setVisibility(View.GONE);
                    }
                }).start();
                break;
            }
        }

    }

    private void commit() {
        StringBuilder pwd = new StringBuilder();
        for (String s : password) {
            if (TextUtils.isEmpty(s)) {
                return;
            }
            pwd.append(s);
        }
        Log.d(TAG, "commit: " + pwd);
        if (mListener != null) {
            mListener.onCommit(pwd.toString());
        } else if (onSetPasswordListener != null) {
            onSetPasswordListener.onCommit(pwd.toString(), payLimit);
        }
        dismiss();
    }

    public interface OnConfirmListener {
        void onCommit(String passWord);
    }

    public interface OnSetPassWordListener {
        void onCommit(String payPassWord, int payLimit);
    }

    public void setOnSetPasswordListener(OnSetPassWordListener onSetPasswordListener) {
        this.onSetPasswordListener = onSetPasswordListener;
    }

    public void setOnConfirmListener(OnConfirmListener mListener) {
        this.mListener = mListener;
    }

}
